package org.kjk.employee;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class EmployeeSearchJSONController extends AbstractController {
	private EmployeeDAOImpl dao;

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String queryType = request.getParameter("type");
		String queryField = request.getParameter("field");

		String sql = null;
		String[] param = null;
		if (queryType == null) {
			sql = "select * from employees";
		} else if (queryType.equals("1")) {
			sql = "select * from employees where LAST_NAME = ?";
			param = new String[] { queryField };
		} else if (queryType.equals("2")) {
			sql = "select * from employees where EMPLOYEE_ID = ?";
			param = new String[] { queryField };
		} else {
			sql = "select * from employees";
		}

		JSONObject json = dao.selectJson(sql, param);
		PrintWriter out = response.getWriter();
		out.println(json);
		return null;
	}

	public void setDao(EmployeeDAOImpl dao) {
		this.dao = dao;
	}
}
